// gadgets.util.registerOnLoadHandler(init);

function init() {
    loadFriends();
}

function loadFriends() {
    var req = opensocial.newDataRequest();
    var params = {};
    params[opensocial.DataRequest.PeopleRequestFields.MAX] = 1000;
    //req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
    req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS',params), 'viewerFriends');
    req.send(onLoadFriends);
}

function onLoadFriends(data) {
    //var viewer = data.get('viewer').getData();
    var viewerFriends = data.get('viewerFriends').getData();

    var friendsList = document.getElementById('eorkut_friendsList');
    if (friendsList == null) {
        var friendsList = document.createElement('div');
        friendsList.setAttribute('id', 'eorkut_friendsList');
        document.getElementById('eorkut_friends').appendChild(friendsList);
    }
    viewerFriends.each(function(person) {
        var friendItem = document.createElement('span');
        friendItem.style.whiteSpace = 'nowrap';
        friendItem.style.borderWidth = '1px';
        friendItem.style.borderStyle = 'solid';
        friendItem.style.borderColor = 'gray';
        friendItem.style.verticalAlign = 'middle';
        // <input type="checkbox" name="eorkut_friend_id[]" value="{person.getId()}" id="eorkut_{person.getId()}" />
        var friendCheckbox = document.createElement('input');
        friendCheckbox.setAttribute('type', 'checkbox');
        friendCheckbox.setAttribute('name', 'eorkut_friend_id[]');
        friendCheckbox.setAttribute('value', person.getId());
        friendCheckbox.setAttribute('id', 'eorkut_friend_' + person.getId());
        friendItem.appendChild(friendCheckbox);
        // <label for="eorkut_{person.getId()}">person.getDisplayName()</label>
        var friendLabel = document.createElement('label');
        friendLabel.setAttribute('for', friendCheckbox.getAttribute('id'));
        var friendImage = document.createElement('img');
        friendImage.setAttribute('src', person.getField(opensocial.Person.Field.THUMBNAIL_URL));
        friendImage.setAttribute('alt', person.getDisplayName());
        friendLabel.appendChild(friendImage);
        friendLabel.appendChild(document.createTextNode(person.getDisplayName()));
        friendItem.appendChild(friendLabel);
        friendsList.appendChild(friendItem);
        friendsList.appendChild(document.createTextNode(" \n"));
    });
    if (viewerFriends.getTotalSize() > viewerFriends.size() + viewerFriends.getOffset()) {
        var req = opensocial.newDataRequest();
        //req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
        var params = {};
        params[opensocial.DataRequest.PeopleRequestFields.MAX] = 1000;
        params[opensocial.DataRequest.PeopleRequestFields.FIRST] = viewerFriends.size() + viewerFriends.getOffset();
        req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS',params), 'viewerFriends');
        req.send(onLoadFriends);
    }
}

function eorkut_sendScraps(send_type) {
    var params = {};
    params[opensocial.Message.Field.TYPE] = send_type;
    params[opensocial.Message.Field.TITLE] = document.getElementById('eorkut_message_title').value;
    var message = opensocial.newMessage(
        document.getElementById('eorkut_message_text').value + '<br/> <p style="font-size:0.6em;">Message sent using eorkut (http://eorkut.googlecode.com).</p>',
        params);
    var friends = new Array();
    for (var i = 0; i < document.forms['eorkut_send_message'].elements.length; i++){
        if (document.forms['eorkut_send_message'].elements[i].name == 'eorkut_friend_id[]' && document.forms['eorkut_send_message'].elements[i].checked){
            friends.push(document.forms['eorkut_send_message'].elements[i].value);
            //document.forms['eorkut_send_message'].elements[i].parentNode.appendChild(document.createTextNode('+'));
        }
    }
    opensocial.requestSendMessage(friends, message, eorkut_onSendScraps);
}

function eorkut_onSendScraps(data) {
    if (data.getErrorCode() == null) {
        var main = document.getElementById('eorkut_main');
        while (main.hasChildNodes()) {
            main.removeChild(main.lastChild);
        }
        main.appendChild(document.createTextNode("Messages sent successfully!"));
    } else {
        main.appendChild(document.createTextNode("Error " + data.getErrorCode() + ": " + data.getErrorMessage()));
    }
}

function eorkut_postScrap() {
    var req = new XMLHttpRequest();
    req.open('POST', 'http://sandbox.orkut.com/Scrapbook.aspx', true);
    req.setRequestHeader("Content-Type",
                           "application/x-www-form-urlencoded");
    req.onreadystatechange = eorkut_onPostScrap;
    var request = 'Action.submit=1&';
    request += 'scrapText=' + encodeURIComponent(document.getElementById('eorkut_message_text').value + '<br/> <p style="font-size:0.6em;">Message sent using eorkut (http://eorkut.googlecode.com).</p>') + '&';
    request += 'uid=4303628935736973096';
    req.send(request);
    
}

function eorkut_onPostScrap(data) {
    if (data.getErrorCode() == null) {
        var main = document.getElementById('eorkut_main');
        while (main.hasChildNodes()) {
            main.removeChild(main.lastChild);
        }
        main.appendChild(document.createTextNode("Messages sent successfully!"));
    } else {
        main.appendChild(document.createTextNode("Error " + data.getErrorCode() + ": " + data.getErrorMessage()));
    }
}
